import 'package:cat_eye/config/string_config.dart';
import 'package:cat_eye/provider/provider_widget.dart';
import 'package:cat_eye/root_page/home_page/hom_video_page_widget.dart';
import 'package:cat_eye/root_page/home_page/home_banner_page_widget.dart';
import 'package:cat_eye/root_page/home_page/video_details_widget.dart';
import 'package:cat_eye/util/loading_state_widget_util.dart';
import 'package:cat_eye/util/util.dart';
import 'package:cat_eye/view_model/home_view_model/home_body_view_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/*
* @Package: root_page.home_page
* @ClassName: home_body_page
* @Author: szj
* @CreateDate: 7/28/21 3:46 PM
*/
class HomeBodyPageWidget extends StatefulWidget {
  const HomeBodyPageWidget({Key key}) : super(key: key);

  @override
  _HomeBodyPageWidgetState createState() => _HomeBodyPageWidgetState();
}

class _HomeBodyPageWidgetState extends State<HomeBodyPageWidget> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<HomeBodyViewModel>(
      model: HomeBodyViewModel(),
      onModelInit: (model) => model.requestHttpData(),
      builder: (context, model, child) {
        return LoadingWidget(
          type: model.type,
          //重新加载
          reload: model.requestHttpData,
          child: Container(
            //上拉加载 下拉刷新参考文档[https://github.com/peng8350/flutter_pulltorefresh/blob/master/README_CN.md]
            child: SmartRefresher(
              footer: CustomFooter(
                builder: (BuildContext context, LoadStatus mode) {
                  Widget body;
                  if (mode == LoadStatus.idle) {
                    body = Text("上拉加载中~~");
                  } else if (mode == LoadStatus.loading) {
                    body = CupertinoActivityIndicator();
                  } else if (mode == LoadStatus.failed) {
                    body = Text("加载失败！点击重试！");
                  } else if (mode == LoadStatus.canLoading) {
                    body = Text("松手,加载更多!");
                  } else {
                    body = Text("没有更多数据了!");
                  }
                  return Container(
                    height: 55.0,
                    child: Center(child: body),
                  );
                },
              ),
              controller: model.refreshController,
              //下拉刷新
              onRefresh: model.requestHttpData,
              //上拉加载更多
              onLoading: model.loadMore,
              //允许下拉
              enablePullDown: true,
              //允许上拉
              enablePullUp: true,
              child: ListView.separated(
                itemBuilder: (BuildContext context, int index) {
                  Widget widget;
                  if (index == 0) {
                    return widget = HomeBannerPageWidget(
                      bannerList: model.bannerList,
                    );
                  }
                  if (model.videoList[index] != null &&
                      model.videoList[index].type ==
                          StringConfig.HOME_TEXT_HEADER) {
                    //
                    widget = _text(model.videoList[index].data.text);
                  } else {
                    widget = HomeVideoPageWidget(video: model.videoList[index]);
                  }
                  return InkWell(
                      onTap: () {
                        Navigator.push(
                            context,
                            router(
                                widget: VideoDetailsWidget(
                              data: model.videoList[index].data,
                            )));
                      },
                      child: widget);
                },

                //ListView 分割线
                separatorBuilder: (BuildContext context, int index) {
                  return Container(
                    alignment: Alignment.center,
                    // color: Color(0xffe6e6e6),
                    color: Colors.red,
                  );
                },
                itemCount: model.videoList.length,
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _text(String title) {
    return Padding(
      padding: EdgeInsets.only(top: 8),
      child: Center(
        child: Text(
          title,
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }
}
